<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理系统 - 个人信息</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Noto Sans SC', Arial, sans-serif;
            background: linear-gradient(135deg, #f6f9fc 0%, #eef2f7 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 1rem;
        }
        .container {
            background-color: white;
            padding: 2.5rem;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 500px;
            position: relative;
            overflow: hidden;
        }
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #2ecc71, #27ae60);
        }
        h2 {
            text-align: center;
            margin-bottom: 2rem;
            color: #2c3e50;
            font-size: 1.8rem;
            font-weight: 700;
        }
        .form-group {
            margin-bottom: 1.5rem;
            position: relative;
        }
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #34495e;
            font-weight: 500;
            font-size: 0.95rem;
        }
        .form-group input {
            width: 100%;
            padding: 1rem;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background-color: #f8f9fa;
        }
        .form-group input:focus {
            outline: none;
            border-color: #2ecc71;
            background-color: white;
            box-shadow: 0 0 0 3px rgba(46, 204, 113, 0.1);
        }
        .form-group input::placeholder {
            color: #95a5a6;
        }
        .form-group input[readonly] {
            background-color: #f1f2f6;
            cursor: not-allowed;
        }
        .password-requirements {
            font-size: 0.85rem;
            color: #7f8c8d;
            margin-top: 0.5rem;
            padding-left: 0.5rem;
        }
        .password-requirements ul {
            list-style: none;
            margin-top: 0.5rem;
        }
        .password-requirements li {
            margin-bottom: 0.3rem;
            display: flex;
            align-items: center;
        }
        .password-requirements li::before {
            content: '•';
            margin-right: 0.5rem;
            color: #95a5a6;
        }
        .password-requirements li.valid {
            color: #2ecc71;
        }
        .password-requirements li.valid::before {
            content: '✓';
            color: #2ecc71;
        }
        .action-buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 2rem;
            gap: 1rem;
        }
        .submit-btn, .back-btn {
            flex: 1;
            padding: 1rem;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .submit-btn {
            background: linear-gradient(135deg, #2ecc71, #27ae60);
            color: white;
        }
        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(46, 204, 113, 0.2);
        }
        .submit-btn:active {
            transform: translateY(0);
        }
        .submit-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        .back-btn {
            background: linear-gradient(135deg, #e74c3c, #c0392b);
            color: white;
        }
        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(231, 76, 60, 0.2);
        }
        .back-btn:active {
            transform: translateY(0);
        }
        .message {
            text-align: center;
            margin-bottom: 1.5rem;
            padding: 1rem;
            border-radius: 8px;
            font-size: 0.95rem;
            display: none;
        }
        .message.error {
            background-color: #ff6b6b;
            color: white;
            display: block;
        }
        .message.success {
            background-color: #2ecc71;
            color: white;
            display: block;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 1rem 0;
        }
        .loading-spinner {
            width: 30px;
            height: 30px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #2ecc71;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        @media (max-width: 480px) {
            .container {
                padding: 2rem 1.5rem;
            }
            h2 {
                font-size: 1.5rem;
            }
            .action-buttons {
                flex-direction: column;
            }
            .submit-btn, .back-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>修改个人信息</h2>
        <div id="message" class="message"></div>
        <form id="profileForm">
            <input type="hidden" id="userId">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" readonly>
            </div>
            <div class="form-group">
                <label for="password">新密码 (留空则不修改)</label>
                <input type="password" id="password" placeholder="请输入新密码">
                <div class="password-requirements">
                    <p>密码要求：</p>
                    <ul>
                        <li id="length">至少8个字符</li>
                        <li id="uppercase">包含大写字母</li>
                        <li id="lowercase">包含小写字母</li>
                        <li id="number">包含数字</li>
                        <li id="special">包含特殊字符</li>
                    </ul>
                </div>
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认新密码</label>
                <input type="password" id="confirmPassword" placeholder="请再次输入新密码">
            </div>
            <div class="loading" id="loading">
                <div class="loading-spinner"></div>
            </div>
            <div class="action-buttons">
                <button type="submit" class="submit-btn" disabled>保存</button>
                <button type="button" class="back-btn" onclick="location.href='index.html'">返回主页</button>
            </div>
        </form>
    </div>

    <script>
        // 显示消息
        function showMessage(message, type = 'error') {
            const messageDiv = document.getElementById('message');
            messageDiv.textContent = message;
            messageDiv.className = `message ${type}`;
            setTimeout(() => {
                messageDiv.style.display = 'none';
            }, 5000);
        }

        // 显示加载动画
        function showLoading() {
            document.getElementById('loading').style.display = 'block';
            document.querySelector('.submit-btn').disabled = true;
        }

        // 隐藏加载动画
        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
            document.querySelector('.submit-btn').disabled = false;
        }

        // 密码验证规则
        const passwordRules = {
            length: password => password.length >= 8,
            uppercase: password => /[A-Z]/.test(password),
            lowercase: password => /[a-z]/.test(password),
            number: password => /[0-9]/.test(password),
            special: password => /[!@#$%^&*]/.test(password)
        };

        // 验证密码
        function validatePassword(password) {
            let isValid = true;
            for (const [rule, validator] of Object.entries(passwordRules)) {
                const element = document.getElementById(rule);
                if (validator(password)) {
                    element.classList.add('valid');
                } else {
                    element.classList.remove('valid');
                    isValid = false;
                }
            }
            return isValid;
        }

        // 验证表单
        function validateForm() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const submitBtn = document.querySelector('.submit-btn');

            if (!password) {
                submitBtn.disabled = false;
                return;
            }

            const isPasswordValid = validatePassword(password);
            const doPasswordsMatch = password === confirmPassword;

            submitBtn.disabled = !(isPasswordValid && doPasswordsMatch);
        }

        // 添加输入事件监听器
        document.getElementById('password').addEventListener('input', validateForm);
        document.getElementById('confirmPassword').addEventListener('input', validateForm);

        // 页面加载时获取用户信息
        document.addEventListener('DOMContentLoaded', fetchUserProfile);

        async function fetchUserProfile() {
            showLoading();
            try {
                const response = await fetch('profile');
                const data = await response.json();

                if (data.success) {
                    document.getElementById('userId').value = data.userId;
                    document.getElementById('username').value = data.username;
                } else {
                    showMessage(data.message || '获取用户信息失败');
                    if (data.message === '未登录') {
                        setTimeout(() => { window.location.href = 'login.html'; }, 1500);
                    }
                }
            } catch (error) {
                console.error('Error fetching user profile:', error);
                showMessage('系统错误，获取用户信息失败');
            } finally {
                hideLoading();
            }
        }

        document.getElementById('profileForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            const userId = document.getElementById('userId').value;
            const newPassword = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (newPassword && newPassword !== confirmPassword) {
                showMessage('两次输入的密码不一致！');
                return;
            }

            if (newPassword && !validatePassword(newPassword)) {
                showMessage('密码不符合要求');
                return;
            }

            showLoading();

            try {
                const response = await fetch('profile', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        userId: userId,
                        newPassword: newPassword
                    })
                });

                const result = await response.json();

                if (result.success) {
                    showMessage('个人信息更新成功！', 'success');
                    document.getElementById('password').value = '';
                    document.getElementById('confirmPassword').value = '';
                    document.querySelector('.submit-btn').disabled = true;
                } else {
                    showMessage(result.message || '个人信息更新失败');
                }
            } catch (error) {
                console.error('Error updating profile:', error);
                showMessage('系统错误，更新失败');
            } finally {
                hideLoading();
            }
        });
    </script>
</body>
</html> 